//    Copyright (C) Clear Office.  All rights reserved.
using System.ComponentModel;
using System.Diagnostics.Contracts;
using System.Reflection;
using Office.Core.Serialization;

namespace Office.Spreadsheet
{
    public class SheetProtection : IOfficeXmlSerializable, INotifyPropertyChanged
    {

        private string _password;

        public string Password
        {
            get { return _password; }
            set { _password = value;notifyPropertyChanged("Password"); }
        }

        public bool Sheet
        {
            get { return _sheet; }
            set { _sheet = value;notifyPropertyChanged("Sheet"); }
        }

        public bool Objects
        {
            get { return _objects; }
            set { _objects = value; notifyPropertyChanged("Objects");}
        }

        public bool Scenarios
        {
            get { return _scenarios; }
            set { _scenarios = value;
                notifyPropertyChanged("Scenarios"); }
        }

        public bool FormatCells
        {
            get { return _formatCells; }
            set { _formatCells = value;notifyPropertyChanged("FormatCells"); }
        }

        public bool FormatColumns
        {
            get { return _formatColumns; }
            set { _formatColumns = value;notifyPropertyChanged("FormatColumns"); }
        }

        public bool FormatRows
        {
            get { return _formatRows; }
            set { _formatRows = value; notifyPropertyChanged("FormatRows");}
        }

        public bool InsertColumns
        {
            get { return _insertColumns; }
            set { _insertColumns = value; notifyPropertyChanged("InsertColumns");}
        }

        public bool InsertRows
        {
            get { return _insertRows; }
            set { _insertRows = value;notifyPropertyChanged("InsertRows"); }
        }

        public bool InsertHyperlinks
        {
            get { return _insertHyperlinks; }
            set { _insertHyperlinks = value; notifyPropertyChanged("InsertHyperlinks");}
        }

        public bool DeleteColumns
        {
            get { return _deleteColumns; }
            set { _deleteColumns = value; notifyPropertyChanged("DeleteColumns");}
        }

        public bool DeleteRows
        {
            get { return _deleteRows; }
            set { _deleteRows = value; notifyPropertyChanged("DeleteRows");}
        }

        public bool SelectLockedCells
        {
            get { return _selectLockedCells; }
            set { _selectLockedCells = value; notifyPropertyChanged("SelectLockedCells");}
        }

        public bool PivotTables
        {
            get { return _pivotTables; }
            set { _pivotTables = value; notifyPropertyChanged("PivotTables");}
        }

        public bool SelectUnlockedCells
        {
            get { return _selectUnlockedCells; }
            set { _selectUnlockedCells = value;notifyPropertyChanged("SelectUnlockedCells"); }
        }

        #region IOfficeXmlSerializable Members

        void IOfficeXmlSerializable.ReadXml(ReadContext context)
        {
            using (ReadContext c = context.Read("sheetProtection"))
            {
                _password = c.GetOptionalString("password");
                Sheet = c.GetOptionalBoolean("sheet", false);
                Objects = c.GetOptionalBoolean("objects", false);
                Scenarios =  c.GetOptionalBoolean("scenarios", false);
                FormatCells = c.GetOptionalBoolean("formatCells", true);
                FormatColumns = c.GetOptionalBoolean("formatColumns", true);
                FormatRows = c.GetOptionalBoolean("formatRows", true);
                InsertColumns = c.GetOptionalBoolean("insertColumns", true);
                InsertRows = c.GetOptionalBoolean("insertRows", true);
                InsertHyperlinks = c.GetOptionalBoolean("insertHyperlinks", true);
                DeleteColumns = c.GetOptionalBoolean("deleteColumns", true);
                DeleteRows = c.GetOptionalBoolean("deleteRows", true);
                SelectLockedCells = c.GetOptionalBoolean("selectLockedCells", false);
                _sort = c.GetOptionalBoolean("sort", true);
                _autoFilter = c.GetOptionalBoolean("autoFilter", true);
                PivotTables = c.GetOptionalBoolean("pivotTables", true);
                SelectUnlockedCells = c.GetOptionalBoolean("selectUnlockedCells", false);
            }
        }

        void IOfficeXmlSerializable.WriteXml(WriteContext context)
        {
            using (WriteContext c = context.Write("sheetProtection"))
            {
                c.SetOptionalString("password", _password);

                c.SetOptionalBoolean("sheet", Sheet, false);
                c.SetOptionalBoolean("objects", Objects, false);
                c.SetOptionalBoolean("scenarios", Scenarios, false);
                c.SetOptionalBoolean("formatCells", FormatCells, true);
                c.SetOptionalBoolean("formatColumns", FormatColumns, true);
                c.SetOptionalBoolean("formatRows", FormatRows, true);
                c.SetOptionalBoolean("insertColumns", InsertColumns, true);
                c.SetOptionalBoolean("insertRows", InsertRows, true);
                c.SetOptionalBoolean("insertHyperlinks", InsertHyperlinks, true);
                c.SetOptionalBoolean("deleteColumns", DeleteColumns, true);
                c.SetOptionalBoolean("deleteRows", DeleteRows, true);
                c.SetOptionalBoolean("selectLockedCells", SelectLockedCells, false);
                c.SetOptionalBoolean("sort", _sort, true);
                c.SetOptionalBoolean("autoFilter", _autoFilter, true);
                c.SetOptionalBoolean("pivotTables", PivotTables, true);
                c.SetOptionalBoolean("selectUnlockedCells", SelectUnlockedCells, false);

            }
        }

        #endregion

        private bool _sheet = false;
        private bool _objects = false;
        private bool _scenarios = false;
        private bool _formatCells = true;
        private bool _formatColumns = true;
        private bool _formatRows = true;
        private bool _insertColumns = true;
        private bool _insertRows = true;
        private bool _insertHyperlinks = true;
        private bool _deleteColumns = true;
        private bool _deleteRows = true;
        private bool _selectLockedCells = false;
        bool _sort = true;
        bool _autoFilter = true;
        private bool _pivotTables = true;
        private bool _selectUnlockedCells = false;

        public event PropertyChangedEventHandler PropertyChanged;

        private void notifyPropertyChanged(string propertyName)
        {
            Contract.Requires(GetType().GetProperty(propertyName, BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic) != null);
            PropertyChangedEventHandler changed = PropertyChanged;
            if (changed != null) changed(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}
